/*  BEGIN_FILE_HDR
******************************************Copyright(C)*****************************************
*
*                                       YKXH  Technology
*
***********************************File Info***************************************************
*   File Name       @: PduR.c
************************************************************************************************
*   Project/Product @: PduR
*   Title           @: 
*   Author          @: zhongliang.Yang 
************************************************************************************************
*   Description     @: 
*                   
************************************************************************************************
*   Limitations     @: None
*
************************************************************************************************
*
************************************************************************************************
*   Revision History:
* 
*   Version     Date          Initials           CR#         Descriptions
*   --------    -----------   ----------------   --------    -----------------------
*   1.0         2018/05/29    zhongliang.yang    N/A         Original 
*   1.1         xxxx/xx/xx    xxxxx              N/A         xxxxxxxx 
*     
************************************************************************************************
* END_FILE_HDR*/

#include "PduR.h"
//#include "CanIf.h"
#include "PduR_Com.h"
//#include "Com_Cbk.h"




#include "../src/BSW/CommS/NM/UDS/CanTp/CanTp.h"
//#include "Dcm_Cbk.h"
#include "../src/BSW/CommS/NM/UDS/Dcm/Dcm_Cbk.h"

extern const CanIfConfMapping   CanIfTxPduConf2ComMapping[];
extern const CanTpConfMapping   CanTpTxPduConf2UpperMapping[];
extern const CanTpIndMapping    CanTpRxPduInd2UpperMapping[];

/*common*/
#define PduRDestType(Indx)                    PduRDestPduHandles[Indx].PduRDestPduRef/*to xx*/
/* com tx  */
#define PduRComTxIdNumOfDest(ComTxId)         PduR_PBConfigStruct.PduRComRoutingTableIndexPtr[ComTxId].NumOfDest
#define PduRComTxIdDestIndex(ComTxId)         PduR_PBConfigStruct.PduRComRoutingTableIndexPtr[ComTxId].PduRTablePtr[i].PduRRoutingPath.PduRDestPduIndex
#define PduRComTxIdSrcIndex(ComTxId)          PduR_PBConfigStruct.PduRComRoutingTableIndexPtr[ComTxId].PduRTablePtr[i].PduRRoutingPath.PduRSrcPduIndex
#define PudRLowerLayerDestHandle(ComTxId)     PduRDestPduHandles[PduRComTxIdDestIndex(ComTxId)].PduRDestPduHandleId 
	
#define PduRComTxConfCounter(ComTxId)         PduR_PBConfigStruct.PduRComRoutPathDestCntPtr[ComTxId]

#define ComTxHandle(CopyTxDataId)             CanTpTxPduConf2UpperMapping[CopyTxDataId].UpperHandle

#define ROUTCOMTOCANIF(ComTxId)               PduRDestType(PduRComTxIdDestIndex(ComTxId))==ToCanIfRef
#define ROUTCOMTOCANTP(ComTxId)               PduRDestType(PduRComTxIdDestIndex(ComTxId))==ToCanTpRef
#define ROUTCOMTOLINIF(ComTxId)               PduRDestType(PduRComTxIdDestIndex(ComTxId))==ToLinIfRef
#define ROUTCOMTOLINTP(ComTxId)               PduRDestType(PduRComTxIdDestIndex(ComTxId))==ToLinTpRef

/*CANTP */
#define CANTPCOPYFROMCOM(MappingId)           CanTpTxPduConf2UpperMapping[MappingId].UpperLayer== ToComRef
#define CANTPCONFTOCOM(MappingId)             CanTpTxPduConf2UpperMapping[MappingId].UpperLayer== ToComRef
#define CANTPCONFTODCM(MappingId)             CanTpTxPduConf2UpperMapping[MappingId].UpperLayer== ToDcmRef

#define CANTPINDTOCOM(MappingId)              CanTpRxPduInd2UpperMapping[MappingId].UpperLayer==ToComRef
#define CANTPINDTODCM(MappingId)              CanTpRxPduInd2UpperMapping[MappingId].UpperLayer==ToDcmRef
#define CANTPINDTOCANTP(MappingId)            CanTpRxPduInd2UpperMapping[MappingId].UpperLayer==ToCanTpRef
#define CanTpRxMapping(CanTpRxId)             CanTpRxPduInd2UpperMapping[CanTpRxId].RxMappingIndex

#define PduRCanTpRxIdNumOfDest(CanTpRxId)     PduR_PBConfigStruct.PduRCanTpRoutingTableIndexPtr[CanTpRxId].NumOfDest////
#define PduRCanTpRxIdDestIndex(CanTpRxId)     PduR_PBConfigStruct.PduRCanTpRoutingTableIndexPtr[CanTpRxId].PduRTablePtr[i].PduRRoutingPath.PduRDestPduIndex
#define PduRCanTpRxIdSrcIndex(CanTpRxId)      PduR_PBConfigStruct.PduRCanTpRoutingTableIndexPtr[CanTpRxId].PduRTablePtr[i].PduRRoutingPath.PduRSrcPduIndex
#define PudRUpperLayerDestHandle(CanTpRxId)   PduRDestPduHandles[PduRCanTpRxIdDestIndex(CanTpRxId)].PduRDestPduHandleId 

#define DcmTxHandle(CopyTxDataId)             CanTpTxPduConf2UpperMapping[CopyTxDataId].UpperHandle


/*CanIf Rx*/   
#define PduRCanIfRxIdNumOfDest(CanIfRxId)     PduR_PBConfigStruct.PduRCanIfRoutingTableIndexPtr[CanIfRxId].NumOfDest
#define PduRCanIfRxIdDestIndex(CanIfRxId)     PduR_PBConfigStruct.PduRCanIfRoutingTableIndexPtr[CanIfRxId].PduRTablePtr[i].PduRRoutingPath.PduRDestPduIndex
#define PduRCanIfRxIdSrcIndex(CanIfRxId)      PduR_PBConfigStruct.PduRCanIfRoutingTableIndexPtr[CanIfRxId].PduRTablePtr[i].PduRRoutingPath.PduRSrcPduIndex
#define PduRCanIfRCanIfConfCounter(CanIfRxId) PduR_PBConfigStruct.PduRCanIfRoutPathDestCntPtr[CanIfRxId]

#define ROUTCANIFTOCANIF(CanIfRxId)           PduRDestType(PduRCanIfRxIdDestIndex(CanIfRxId))==ToCanIfRef
#define ROUTCANIFTOCOM(CanIfRxId)             PduRDestType(PduRCanIfRxIdDestIndex(CanIfRxId))==ToComRef
#define ROUTCANIFTOFRIF(CanIfRxId)            PduRDestType(PduRCanIfRxIdDestIndex(CanIfRxId))==ToFrIfRef
#define ROUTCANIFTOLINIF(CanIfRxId)           PduRDestType(PduRCanIfRxIdDestIndex(CanIfRxId))==ToLinTpRef

#define PudRComDestHandle(LowRxId)            PduRDestPduHandles[PduRCanIfRxIdDestIndex(LowRxId)].PduRDestPduHandleId 


static uint8 PduRComRoutPathDestCounter[] = {0};
static uint8 PduRDcmRoutPathDestCounter[] = {0};
static uint8 PduRCanIfRoutPathDestCounter[] = {0};
static uint8 PduRCanTpRoutPathDestCounter[] = {0};

const PduR_PBConfigType PduR_PBConfigStruct = 
{
  PduRComRoutingTableIndex,
  PduRDcmRoutingTableIndex,
  PduRCanIfRoutingTableIndex,
  PduRCanTpRoutingTableIndex,
  PduRComRoutPathDestCounter,
  PduRDcmRoutPathDestCounter,
  PduRCanIfRoutPathDestCounter,
  PduRCanTpRoutPathDestCounter,
};



//static const PduRRoutingTableIndexType*     gPduRRoutTblBootPtr = ((void *) 0);
//static const CanIfConfMapping*              gPduRCanIfTxConfMapPtr = ((void *) 0);
//static       ComTxConfCnt_Type              ComTxConfCnt[]= {0};
//static       ComTxConfCnt_Type*             gComTxConfCntPtr = ((void *) 0);

/*BEGIN_FUNCTION_HDR
***********************************************************************************************
 
Service name:       PduR_Init  
Syntax:             void PduR_Init
                    ( 
                        const PduR_PBConfigType* ConfigPtr 
                    )
Service ID[hex]:       
Sync/Async:         Synchronous   
Reentrancy:         Reentrant 
Parameters (in):    None  
Parameters (inout): None 
Parameters (out):   None  
Return value:       None  
Description:        This function is used for abort all the message in the transmission buffer
                    which is belong to one controller
***********************************************************************************************
END_FUNCTION_HDR*/
void PduR_Init
( 
    const PduR_PBConfigType* ConfigPtr 
)
{
//   uint16 i;
}

#if(PDUR_COM_SUPPORT == STD_ON)
/* BEGIN_FUNCTION_HDR
***********************************************************************************************
 
Service name:       PduR_ComTransmit  
Syntax:             Std_ReturnType PduR_ComTransmit
                    (
                        PduIdType id,
                        PduInfoType* info
                    )
Service ID[hex]:       
Sync/Async:         Synchronous   
Reentrancy:         Reentrant 
Parameters (in):    None  
Parameters (inout): None 
Parameters (out):   None  
Return value:       None  
Description:        This function is used for abort all the message in the transmission buffer
                    which is belong to one controller
***********************************************************************************************
END_FUNCTION_HDR*/
Std_ReturnType PduR_ComTransmit
(
    PduIdType id,
    PduInfoType* info
)
{
    Std_ReturnType result = E_NOT_OK;

    if(id < 1/*COM_TXIPDUNUM*/) 
    {
   
        uint8 i;	
        
        PduRComTxConfCounter(id)=0; 
                    
        for(i = 0;i < PduRComTxIdNumOfDest(id);i++)
        {                                         
            if(ROUTCOMTOCANIF(id))
            {
                if(E_OK == CanIf_Transmit(PudRLowerLayerDestHandle(id), info))
                {
                    result = E_OK;
                }
            }
            else if(ROUTCOMTOCANTP(id))
            {
                if(E_OK == CanTp_Transmit(PudRLowerLayerDestHandle(id), info))
                {
                    result = E_OK;
                }
            }
            else
            {
            }
        }
    }
    else
    {
        result = E_NOT_OK;
    }

    return result;
}

#endif

#if(PDUR_CANIF_SUPPORT == STD_ON)
/* BEGIN_FUNCTION_HDR
***********************************************************************************************
 
Service name:       PduR_CanIfRxIndication  
Syntax:             void PduR_CanIfRxIndication
                    (
                        PduIdType id,
                        PduInfoType* buffer
                    )
Service ID[hex]:       
Sync/Async:         Synchronous   
Reentrancy:         Reentrant 
Parameters (in):    None  
Parameters (inout): None 
Parameters (out):   None  
Return value:       None  
Description:        This function is used for abort all the message in the transmission buffer
                    which is belong to one controller
***********************************************************************************************
END_FUNCTION_HDR*/
FUNC(void, PDUR_CODE) PduR_CanIfRxIndication
                      (
                        PduIdType id,
                        P2CONST(PduInfoType, AUTOMATIC, PDUR_APPL_DATA) info
                      )                 
{

   if(id < PduR_NumOfCanIfRxObj) 
   {
      uint8 i;
      
      PduRCanIfRCanIfConfCounter(id)=0; 
                    
      for(i = 0;i < PduRCanIfRxIdNumOfDest(id);i++)
      {                                         
          if(ROUTCANIFTOCOM(id))
          {
             //Com_RxIndication(PudRComDestHandle(id),info);
          }
          else if(ROUTCANIFTOCANIF(id))
          {
             if(E_OK == CanIf_Transmit(PudRLowerLayerDestHandle(id), info))
             {
             }
          }
          else if(ROUTCANIFTOLINIF(id)) /* to LinIf */
          {
             #if 0 
              if(E_OK == LinIf_Transmit(PudRLowerLayerDestHandle(id), info))
              {
              }
             #endif
          }
          else
          {
          
          }
      }
   }
   else
   {
   }

   /*return result;*/
}

/* BEGIN_FUNCTION_HDR
***********************************************************************************************
Service name:       PduR_CanIfTxConfirmation  
Syntax:             void PduR_CanIfTxConfirmation
                    ( 
                        PduIdType id 
                    )
Service ID[hex]:       
Sync/Async:         Synchronous   
Reentrancy:         Reentrant 
Parameters (in):    None  
Parameters (inout): None 
Parameters (out):   None  
Return value:       None  
Description:        This function is used for abort all the message in the transmission buffer
                    which is belong to one controller
***********************************************************************************************
END_FUNCTION_HDR*/

void PduR_CanIfTxConfirmation
( 
    PduIdType id         
)
{
//    uint8 i=0;
     
    if(id < 1/*COM_TXIPDUNUM*/)
    {
        PduRComTxConfCounter(id)++;       
        if(PduRComTxConfCounter(id) == PduRComTxIdNumOfDest(id))
        {
            //Com_TxConfirmation(id);
            PduRComTxConfCounter(id)=0;
        }
    }
}
#endif


# if(PDUR_CANTP_SUPPORT == STD_ON)
/**********************************************************************************************************************
| NAME:             PduR_CanTpProvideTxBuffer
| CALLED BY:        CAN Transport Protocol
| DESCRIPTION:      See PDUR356 for a complete API description
**********************************************************************************************************************/
/*fan debug*/
extern uint8  CanTpRxDebug[20];
extern uint8  CanTpTxDebug[20];

BufReq_ReturnType PduR_CanTpCopyTxData
                                      (
                                        PduIdType id,  
                                        PduInfoType* info,
                                        RetryInfoType* retry,
                                        PduLengthType* availableDataPtr
                                      )
{     
    BufReq_ReturnType result = BUFREQ_E_NOT_OK;
     
//    PduIdType  UpperLayerId;
    
    uint8 i = 0;
#if 0
/*fan debug*/    
    for(i = 0; i < (* availableDataPtr); i++)
    {
        info->SduDataPtr[i] = CanTpTxDebug[i] ;
    }
    
    result = BUFREQ_OK;  
#endif
    
    
#if 1   
    if(PduRCanTpDcmTxID == id)
    {
        result = Dcm_CopyTxData(PduRCanTpDcmTxID,info,retry, availableDataPtr); 
    }
    else if(CANTPCOPYFROMCOM(id))
    {
        for(i = 0;i<PduRComTxIdNumOfDest(ComTxHandle(id));i++)
        {
            if(id == PudRLowerLayerDestHandle(ComTxHandle(id)))
            {       
                //result=Com_CopyTxData(ComTxHandle(id),info,retry, availableDataPtr);
                i = PduRComTxIdNumOfDest(ComTxHandle(id));
            }
            else
            {
        	
            }
        }
    }
    else
    {
    }
 #endif
    return result;   /*Buffer request accomplished successful.*/                 
}
/**********************************************************************************************************************
| NAME:             PduR_CanTpTxConfirmation
| CALLED BY:        CAN Transport Protocol
| DESCRIPTION:      See PDUR359 for a complete API description
**********************************************************************************************************************/
void PduR_CanTpTxConfirmation (PduIdType id, NotifResultType Result)
{
//    BufReq_ReturnType result=BUFREQ_E_NOT_OK;
     
//    PduIdType  UpperLayerId;
    
//    uint8 i = 0;
 
    if(PduRCanTpDcmTxID == id)
    {
        Dcm_TxConfirmation(PduRCanTpDcmTxID,Result); 
    }
    else if(CANTPCONFTOCOM(id))
    {   
      
        PduRComTxConfCounter(id)++;
      
        if(PduRComTxConfCounter(id) == PduRComTxIdNumOfDest(ComTxHandle(id)))
        {
            //Com_TpTxConfirmation(ComTxHandle(id),Result);
            PduRComTxConfCounter(id)=0;
        }    
    }    
    else
    {
    }                     
}

/**********************************************************************************************************************
| NAME:             PduR_CanTpStartOfReception
| CALLED BY:        CAN Transport Protocol
| DESCRIPTION:      See PDUR350 for a complete API description
**********************************************************************************************************************/
BufReq_ReturnType PduR_CanTpStartOfReception
                                      (
                                        PduIdType id,
                                        PduLengthType TpSduLength,
                                        PduLengthType* bufferSizePtr
                                      )
{

    BufReq_ReturnType result=BUFREQ_E_NOT_OK;
//    uint8 i = 0;
    
#if 0
/*fan debug*/
    result = BUFREQ_OK;
#endif
   
#if 1
    
    if(PduRCanTpDcmRxPhy == id)
    {
        result = Dcm_StartOfReception(UDS_PHYSICAL_ON_CAN_Rx,TpSduLength,bufferSizePtr);	
    }
    else if(PduRCanTpDcmRxFun == id)
    {
        result = Dcm_StartOfReception(UDS_FUNCTIONAL_ON_CAN_Rx,TpSduLength,bufferSizePtr);	
    }
    
    #if 0
    else if(CANTPINDTOCOM(id))
    {
        result = Com_StartOfReception(PudRUpperLayerDestHandle((CanTpRxMapping(id))),TpSduLength,bufferSizePtr);	
    }
    #endif
    
    else if(CANTPINDTOCANTP(id))
    {
     /* TBD */	
    }
    else
    {
    	
    }
 #endif
 
    return result;
}
/**********************************************************************************************************************
| NAME:             PduR_CanTpCopyRxData
| CALLED BY:        CAN Transport Protocol
| DESCRIPTION:      See PDUR350 for a complete API description
**********************************************************************************************************************/
BufReq_ReturnType PduR_CanTpCopyRxData
                                      (
                                        PduIdType id,
                                        PduInfoType* info,
                                        PduLengthType* bufferSizePtr
                                      )
{
    BufReq_ReturnType result=BUFREQ_E_NOT_OK;
//    uint8 i = 0;
    
#if 0
/*fan debug*/
    for(i = 0; i < 20; i++)
    {
         CanTpRxDebug[i] = info->SduDataPtr[i] ;
    }
    result = BUFREQ_OK;
#endif
 
 
    
#if 1
    
    if(PduRCanTpDcmRxPhy == id)
    {
        result = Dcm_CopyRxData(UDS_PHYSICAL_ON_CAN_Rx,info,bufferSizePtr);	
    }
    else if(PduRCanTpDcmRxFun == id)
    {
        result = Dcm_CopyRxData(UDS_FUNCTIONAL_ON_CAN_Rx,info,bufferSizePtr);	
    }
    else if(CANTPINDTOCOM(id))
    {
        //result = Com_CopyRxData(PudRUpperLayerDestHandle((CanTpRxMapping(id))),info,bufferSizePtr);	
    } 
    else if(CANTPINDTOCANTP(id))
    {	
    }
    else
    {
    	
    }
#endif
    return result; 
                     
}
                                      
/**********************************************************************************************************************
| NAME:             PduR_CanTpRxIndication
| CALLED BY:        CAN Transport Protocol
| DESCRIPTION:      See PDUR353 for a complete API description
**********************************************************************************************************************/
void PduR_CanTpRxIndication(PduIdType id, NotifResultType Result)
{  
//    uint8 i = 0;

    if(PduRCanTpDcmRxPhy == id)
    {
        Dcm_RxIndication(UDS_PHYSICAL_ON_CAN_Rx,Result);	
    }
    else if(PduRCanTpDcmRxFun == id)
    {
        Dcm_RxIndication(UDS_FUNCTIONAL_ON_CAN_Rx,Result);	
    }
    else if(CANTPINDTOCOM(id))
    {
        //Com_TpRxIndication(PudRUpperLayerDestHandle((CanTpRxMapping(id))),Result);	
    }
    else if(CANTPINDTOCANTP(id))
    {	
    }
    else
    {
    	
    }
}
#endif

#if(PDUR_DCM_SUPPORT == STD_ON)
/**********************************************************************************************************************
| NAME:             PduR_DcmTransmit
| CALLED BY:        DCM
| DESCRIPTION:      See PDUR408 for a complete API description
**********************************************************************************************************************/
Std_ReturnType PduR_DcmTransmit(PduIdType id,PduInfoType* info)                                          
{
    Std_ReturnType result = E_OK;
                                
    (void*)id;
    
    if(E_OK != CanTp_Transmit(PduRCanTpDcmTxID, info))
    {
        result = E_NOT_OK;
    }
    
    return result;
}
#endif
                                
